{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.输入某年某月某日，判断这一天是这一年的第几天？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年11月28日是这一年的第333天。\n"
     ]
    }
   ],
   "source": [
    "year = int(input(\"请输入年份：\"))\n",
    "month = int(input(\"请输入月份：\"))\n",
    "day = int(input(\"请输入日期：\"))\n",
    "\n",
    "months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "if (year % 4 == 0 and year % 100!= 0) or year % 400 == 0:\n",
    "    months[2] = 29\n",
    "\n",
    "days = 0\n",
    "for i in range(1, month):\n",
    "    days += months[i]\n",
    "\n",
    "days += day\n",
    "\n",
    "print(f\"{year}年{month}月{day}日是这一年的第{days}天。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.判断1000中有多少个素数，并输出所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的素数个数为: 168\n",
      "1000以内的所有素数: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def is_prime(n):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    if n <= 3:\n",
    "        return True\n",
    "    if n % 2 == 0 or n % 3 == 0:\n",
    "        return False\n",
    "    i = 5\n",
    "    while i * i <= n:\n",
    "        if n % i == 0 or n % (i + 2) == 0:\n",
    "            return False\n",
    "        i += 6\n",
    "    return True\n",
    "\n",
    "count = 0\n",
    "prime_numbers = []\n",
    "for num in range(2, 1001):\n",
    "    if is_prime(num):\n",
    "        count += 1\n",
    "        prime_numbers.append(num)\n",
    "\n",
    "print(\"1000以内的素数个数为:\", count)\n",
    "print(\"1000以内的所有素数:\", prime_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该成绩对应的等级为：B\n"
     ]
    }
   ],
   "source": [
    "score = float(input(\"请输入学习成绩：\"))\n",
    "\n",
    "grade = \"A\" if score >= 90 else \"B\" if 60 <= score < 90 else \"C\"\n",
    "\n",
    "print(f\"该成绩对应的等级为：{grade}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数：2\n",
      "空格个数：0\n",
      "数字个数：1\n",
      "其它字符个数：0\n"
     ]
    }
   ],
   "source": [
    "string = input(\"请输入一行字符串：\")\n",
    "\n",
    "letters = 0\n",
    "spaces = 0\n",
    "digits = 0\n",
    "others = 0\n",
    "\n",
    "for char in string:\n",
    "    if char.isalpha():\n",
    "        letters += 1\n",
    "    elif char.isspace():\n",
    "        spaces += 1\n",
    "    elif char.isdigit():\n",
    "        digits += 1\n",
    "    else:\n",
    "        others += 1\n",
    "\n",
    "print(f\"英文字母个数：{letters}\")\n",
    "print(f\"空格个数：{spaces}\")\n",
    "print(f\"数字个数：{digits}\")\n",
    "print(f\"其它字符个数：{others}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "28\n",
      "496\n"
     ]
    }
   ],
   "source": [
    "for num in range(1, 1001):\n",
    "    factors_sum = 0\n",
    "    for i in range(1, num):\n",
    "        if num % i == 0:\n",
    "            factors_sum += i\n",
    "\n",
    "    if num == factors_sum:\n",
    "        print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2!+3!+...+50!的值为：31035053229546199656252032972759319953190362094566672920420940313\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入任意数N：\"))\n",
    "\n",
    "factorial_sum = 0\n",
    "factorial = 1\n",
    "\n",
    "for i in range(1, n + 1):\n",
    "    factorial *= i\n",
    "    factorial_sum += factorial\n",
    "\n",
    "print(f\"1+2!+3!+...+{n}!的值为：{factorial_sum}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.输入任意奇数N，按如下要求打印出图案（菱形）    *  *** ************（N控制行数和本行的星数） *****  ***   *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    *\n",
      "   ***\n",
      "  *****\n",
      " *******\n",
      "*********\n",
      " *******\n",
      "  *****\n",
      "   ***\n",
      "    *\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入任意奇数N：\"))\n",
    "\n",
    "mid_row = n // 2 + 1\n",
    "\n",
    "for i in range(1, n + 1, 2):\n",
    "    spaces = abs(mid_row - (i + 1) // 2)\n",
    "    stars = i\n",
    "    print(\" \" * spaces + \"*\" * stars)\n",
    "\n",
    "for i in range(n - 2, 0, -2):\n",
    "    spaces = abs(mid_row - (i + 1) // 2)\n",
    "    stars = i\n",
    "    print(\" \" * spaces + \"*\" * stars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.对10个数进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的数组： [0.0, 1.0, 2.0, 5.0, 6.0, 7.0, 9.0, 19.0, 23.0, 25.0]\n"
     ]
    }
   ],
   "source": [
    "nums = []\n",
    "for i in range(10):\n",
    "    num = float(input(f\"请输入第{i + 1}个数：\"))\n",
    "    nums.append(num)\n",
    "\n",
    "nums.sort()\n",
    "\n",
    "print(\"排序后的数组：\", nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 20]\n"
     ]
    }
   ],
   "source": [
    "def insert_num_into_sorted_array(sorted_array, num):\n",
    "    for i in range(len(sorted_array)):\n",
    "        if num <= sorted_array[i]:\n",
    "            sorted_array.insert(i, num)\n",
    "            return sorted_array\n",
    "    sorted_array.append(num)\n",
    "    return sorted_array\n",
    "\n",
    "# 示例用法\n",
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n",
    "num_to_insert = int(input(\"请输入一个数: \"))\n",
    "new_sorted_array = insert_num_into_sorted_array(sorted_array, num_to_insert)\n",
    "print(\"插入后的数组:\", new_sorted_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第4号的那位\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入人数n: \"))\n",
    "\n",
    "# 创建一个包含n个人编号的列表\n",
    "circle = list(range(1, n + 1))\n",
    "\n",
    "# 初始报数索引和报数计数器\n",
    "index = 0\n",
    "count = 1\n",
    "\n",
    "while len(circle) > 1:\n",
    "    if count % 3 == 0:\n",
    "        # 如果报到3，就移除对应的人\n",
    "        circle.pop(index)\n",
    "        # 因为移除了一个人，索引需要回退一位，防止跳过下一个人\n",
    "        index -= 1\n",
    "    count += 1\n",
    "    index += 1\n",
    "    # 当索引超出当前圈子人数范围时，重置为0\n",
    "    if index >= len(circle):\n",
    "        index = 0\n",
    "\n",
    "print(\"最后留下的是原来第{}号的那位\".format(circle[0]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
